var co = require('co');
var seqModule = require('../../../routes/createModules');
var nodeExcel = require('excel-export');
var seq = new seqModule();
var util = require('../../../common/util');
var formidable = require('formidable');
var fs = require('fs');
var moment = require("moment");

//报名
var signUpController = {
    //获取报名
    getSignUpList: function (req, res) {
        co(function () {
            var paramD = [];
            var params = {};
            var paramT = {};
            var page = req.query.page;
            var rows = req.query.rows;
            var paramV = { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] };
            if (req.query.pTitle != undefined && req.query.pTitle != "") {
                paramV["where"] = { "$or": [{ "name": { "$like": "%" + req.query.pTitle + "%" } }, { "tel": { "$like": "%" + req.query.pTitle + "%" } }, { "hospitalName": { "$like": "%" + req.query.pTitle + "%" } }] };
            }
            if (req.query.startTime != undefined && req.query.startTime != "") {
                paramD.add({ "createTime": { "$gt": req.query.startTime } });
            }
            if (req.query.endTime != undefined && req.query.endTime != "") {
                paramD.add({ "createTime": { "$lt": req.query.endTime } });
            }
            params["$and"] = paramD;
            params["liveId"] = req.params.liveId;
            var include = [paramV];
            paramT.include = include;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            if (page != undefined && rows != undefined) {
                paramT["offset"] = parseInt(page);
                paramT["limit"] = parseInt(rows);
            }
            seq.signupLogModule.findAndCountAll(paramT).then(function (result) {
                res.json({ "success": true, "page": page, "total": result.count, "result": result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     * 根据 ID 导出数据
     * @param {*} req
     * @param {*} res
     */
    exportByID: function (req, res) {
        co(function () {
            var excelName = "signUpExport";
            var paramD = [];
            var params = {};
            var paramT = {};
            var paramV = { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] }
            if (req.query.pTitle != undefined && req.query.pTitle != "") {
                paramV["where"] = { "$or": [{ "name": { "$like": "%" + req.query.pTitle + "%" } }, { "tel": { "$like": "%" + req.query.pTitle + "%" } }, { "hospitalName": { "$like": "%" + req.query.pTitle + "%" } }] };
            }
            if (req.query.startTime != undefined && req.query.startTime != "") {
                paramD.add({ "createTime": { "$gt": req.query.startTime } });
            }
            if (req.query.endTime != undefined && req.query.endTime != "") {
                paramD.add({ "createTime": { "$lt": req.query.endTime } });
            }
            params["$and"] = paramD;
            params["liveId"] = req.params.liveId;
            var include = [paramV];
            paramT.include = include;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            seq.signupLogModule.findAll(paramT).then(function (result) {
                var conf = {};
                conf.stylesXmlFile = "styles.xml";//注意：这个文件需要去我刚刚发的那个网址上下载一个下来
                conf.name = excelName;
                conf.cols = [
                    {
                        caption: '姓名',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '手机号',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '医院',
                        type: 'string',
                        width: 25
                    }, {
                        caption: '报名时间',
                        type: 'string',
                        width: 10
                    }
                ];


                var vac = new Array();
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    var temp = new Array();
                    if (val.member != undefined && val.member != null) {
                        temp[0] = val.member.name;
                        temp[1] = val.member.tel;
                        temp[2] = val.member.hospitalName;
                    } else {
                        temp[0] = "";
                        temp[1] = "";
                        temp[2] = "";
                    }
                    temp[3] = val.createTime || "";

                    vac.push(temp);

                }
                conf.rows = vac;//conf.rows只接受数组
                var results = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "signExport.xlsx");
                res.end(results, 'binary');
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '导出失败' });
            console.log(e);
        });

    },


    /**
     * 根据 ID 删除评论
     * @param {*} req
     * @param {*} res
     */
    deleteByID: function (req, res) {
        var params = req.query.params;
        var arr = params.split(",");
        try {
            seq.signupLogModule.destroy({
                where: {
                    'id': [arr]
                }
            }).then(function (result) {
                res.json({
                    msg: "删除成功！",
                    success: true
                });
            })
        } catch (err) {
            res.json({
                message: "服务错误！",
                success: false
            });
        }
    },

    /**
     * 所有报名记录查询
     */

    getSignUpDataList: function (req, res) {
        co(function () {
            var paramD = [];
            var params = {};
            var paramT = {};
            var page = req.query.page;
            var rows = req.query.rows;
            var paramP = { model: seq.liveModule, attributes: ['title'] };
            var paramV = { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] };
            if (req.query.pTitle != undefined && req.query.pTitle != "") {
                paramV["where"] = { "$or": [{ "name": { "$like": "%" + req.query.pTitle + "%" } }, { "tel": { "$like": "%" + req.query.pTitle + "%" } }, { "hospitalName": { "$like": "%" + req.query.pTitle + "%" } }] };
                // paramP["where"] = { "title": { "$like": "%" + req.query.pTitle + "%" } };
            }
            if (req.query.title != undefined) {
                paramP["where"] = { "title": { "$like": "%" + req.query.title + "%" } };
            }
            if (req.query.startTime != undefined && req.query.startTime != "") {
                paramD.add({ "createTime": { "$gt": req.query.startTime } });
            }
            if (req.query.endTime != undefined && req.query.endTime != "") {
                paramD.add({ "createTime": { "$lt": req.query.endTime } });
            }
            if (req.query.sort != undefined) {
                params.sort = parseInt(req.query.sort);
            }
            params["$and"] = paramD;
            var include = [paramP, paramV];
            paramT.include = include;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            if (page != undefined && rows != undefined) {
                paramT["offset"] = parseInt(page);
                paramT["limit"] = parseInt(rows);
            }
            seq.signupLogModule.findAndCountAll(paramT).then(function (result) {
                res.json({ "success": true, "page": page, "total": result.count, "result": result.rows });
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },

    /**
     * 所有报名记录导出
     */
    getSignUpExportDataList: function (req, res) {
        co(function () {
            var excelName = "signUpExport";
            var paramD = [];
            var params = {};
            var paramT = {};
            var paramP = { model: seq.liveModule, attributes: ['title'] };
            var paramV = { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] }
            if (req.query.pTitle != undefined && req.query.pTitle != "") {
                paramV["where"] = { "$or": [{ "name": { "$like": "%" + req.query.pTitle + "%" } }, { "tel": { "$like": "%" + req.query.pTitle + "%" } }, { "hospitalName": { "$like": "%" + req.query.pTitle + "%" } }] };
                //paramP["where"] = { "title": { "$like": "%" + req.query.pTitle + "%" } };
            }
            if (req.query.title != undefined) {
                paramP["where"] = { "title": { "$like": "%" + req.query.title + "%" } };
            }
            if (req.query.startTime != undefined && req.query.startTime != "") {
                paramD.add({ "createTime": { "$gt": req.query.startTime } });
            }
            if (req.query.endTime != undefined && req.query.endTime != "") {
                paramD.add({ "createTime": { "$lt": req.query.endTime } });
            }
            if (req.query.sort != undefined) {
                params.sort = parseInt(req.query.sort);
            }
            params["$and"] = paramD;
            var include = [paramP, paramV];
            paramT.include = include;
            paramT["where"] = params;
            paramT["order"] = [['createTime', 'desc']];
            seq.signupLogModule.findAll(paramT).then(function (result) {
                var conf = {};
                conf.stylesXmlFile = "styles.xml";//注意：这个文件需要去我刚刚发的那个网址上下载一个下来
                conf.name = excelName;
                conf.cols = [
                    {
                        caption: '标题',
                        type: 'string',
                        width: 10
                    },
                    {
                        caption: '姓名',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '手机号',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '医院',
                        type: 'string',
                        width: 25
                    }, {
                        caption: '科室',
                        type: 'string',
                        width: 25
                    }, {
                        caption: '报名时间',
                        type: 'string',
                        width: 10
                    }
                ];


                var vac = new Array();
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    var temp = new Array();
                    if (val.live != undefined && val.live != null) {
                        temp[0] = val.live.title;
                    } else {
                        temp[0] = "";
                    }
                    if (val.member != undefined && val.member != null) {
                        temp[1] = val.member.name;
                        temp[2] = val.member.tel;
                        temp[3] = val.member.hospitalName;
                        temp[4] = val.member.hospitalOffices;
                    } else {
                        temp[1] = "";
                        temp[2] = "";
                        temp[3] = "";
                        temp[4] = "";
                    }
                    temp[5] = val.createTime || "";

                    vac.push(temp);

                }
                conf.rows = vac;//conf.rows只接受数组
                var results = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "signExport.xlsx");
                res.end(results, 'binary');
            });
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '导出失败' });
            console.log(e);
        });

    },



}
module.exports = signUpController;